package com.hbxy.favorite.controller;


import com.hbxy.favorite.domain.BackUser;
import com.hbxy.favorite.domain.Follows;
import com.hbxy.favorite.domain.User;
import com.hbxy.favorite.resault.ResponseBo;
import com.hbxy.favorite.service.UserService;
import com.hbxy.favorite.utils.MD5Utils;
import com.hbxy.favorite.utils.RegUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
public class UserController extends BaseController{


    @Autowired
    private UserService userService;


    private Logger log = LoggerFactory.getLogger(this.getClass());
    /**
     *登录接口
     * @param
     * @return
     */

        @RequestMapping("/login" )
        @ResponseBody
        public ResponseBo login(@RequestBody User user){

            if(RegUtil.isNotNull(user.getUserName()) && RegUtil.isNotNull(user.getPassWord())) {

                String password = MD5Utils.encrypt(user.getPassWord()); //md5
            User user1 = userService.findByUsername(user.getUserName());
            User user2 = userService.findByEmail(user.getUserName());

            ResponseBo responseBo1 = judgeLogin(user1, password);
            ResponseBo responseBo2 = judgeLogin(user2, password);

            if (responseBo1 != null) {
                return responseBo1;
            } else if (responseBo2 != null)
                return responseBo2;
            else
                return ResponseBo.error("账号错误");
        }
        return ResponseBo.error("请输入账号或密码");
    }

    /**
     * 获取用户信息接口
     */
    @RequestMapping("/index")
    @ResponseBody
    public ResponseBo  getuserMessage(@RequestBody User user){

        ResponseBo responseBo = userService.getUserMessage(user);
        return responseBo;
    }

    /**
     * 注册接口
     */
    @ResponseBody
    @PostMapping(value = "/register")
    public ResponseBo register(@RequestBody  User user){

        ResponseBo responseBo = userService.register(user);

        return responseBo;

    }

    /**
     * 查询用户粉丝列表
     */
    @RequestMapping("/getFuns")
    public String getFuns(Long userId, Model model){

        List<User> userList = userService.getUsers(userId);

        model.addAttribute("myfansList",userList);

        return "page-personal::personal-b";
    }

    /**
     * 查询用户关注列表
     */
    @RequestMapping("/getFollows")
    public String getFollows(Long userId, Model model){

        List<User> userList = userService.getFollows(userId);
        log.info("关注用户列表："+userList);
        model.addAttribute("followList",userList);

        return "page-follow-personal::personal-c";
    }

    /**
     * 搜索关注
     */
    @RequestMapping("/getUserSearch")
    public String getUserSearch(String userName, Model model){

        List<User> userList = userService.getUserSearch(userName);
        log.info("查询搜索结果："+userList);
        model.addAttribute("searchList",userList);

        return "page-search-personal::personal-s";
    }



    /**
     * 获取个人资料接口
     */
    @RequestMapping("/getMyPersonal")
    public String getMyPersonal(Long id ,Model model){

        User user =  userService.getMyPersional(id);
        log.info("用户名："+user.getUserName());
        model.addAttribute("userName",user.getUserName());
        model.addAttribute("email",user.getEmail());
        model.addAttribute("id",user.getId());
        return "page-mypersonal::mypersonal-b";
    }
    /**
     * 关注别人
     */
    @RequestMapping("/followOnePersonal")
    public String followOnePersonal(Long id ,Model model){
        User user =  userService.getOnePersional(id);
        model.addAttribute("userName",user.getUserName());
        model.addAttribute("email",user.getEmail());
        model.addAttribute("id",user.getId());
        return "page-allpersonal::allpersonal-b";
    }

    @RequestMapping("/toFollow")
    @ResponseBody
    public  ResponseBo toFollow(@RequestBody Follows follows){

        ResponseBo responseBo =userService.addFollow(follows);
        return  responseBo;
    }

    /**
     * 取消关注
     * @param follows
     * @return
     */
    @RequestMapping("/qxFollow")
    @ResponseBody
    public  ResponseBo qxFollow(@RequestBody Follows follows){

        ResponseBo responseBo =userService.qxFollow(follows);
        return  responseBo;
    }

    /**
     * 修改用户资料
     */

    @RequestMapping("/updateuser")
    @ResponseBody
    public  ResponseBo updateUser(@RequestBody User user){

        ResponseBo responseBo =userService.updateUser(user);
        return  responseBo;
    }

    /**
     * 修改密码
     */

    @RequestMapping("/updatepassword")
    @ResponseBody
    public  ResponseBo updatePassword(@RequestBody User user){

        ResponseBo responseBo =userService.updatePassword(user);
        return  responseBo;
    }
}
